package com.atguigu.myjuc.pool;

import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        //自定义
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),//核
                Runtime.getRuntime().availableProcessors() + 1,//cpu密集，这放的是几个线程
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),//等候区三把椅子
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()//DiscardOldestPolicy拒接策略。，所以不会报，不然最大就八个人就爆了
        );

        try {
            for (int i = 1; i <= 20; i++) {//二十个人办理业务
                int finalI = i;
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t"+"为："+ finalI +"\t客户办理业务");
                });
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            threadPool.shutdown();
        }
    }

/*    private static void defaultPool(){
        ExecutorService threadPool = Executors.newFixedThreadPool(3);//一池固定数
        //ExecutorService threadPool2 = Executors.newSingleThreadExecutor();//一池1线程
        //ExecutorService threadPool3 = Executors.newCachedThreadPool();//一池N个线程

        try
        {
            for (int i = 0; i < 20; i++) { //20个顾客（请求线程）来办理业务
                int finalI = i;
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName()+"\t"+"为："+ finalI +"\t客户办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }*/
}
